import 'dart:async';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_device_type/flutter_device_type.dart';
import 'package:tv_friso_screen/login_phone_pad_page.dart';
import 'package:tv_friso_screen/pad/home_main_page_pad.dart';
import 'package:tv_friso_screen/utils/share_pre_utils.dart';

import 'TV/home_main_page_tv.dart';
import 'login_tv_page.dart';
import 'phone/home_main_phone_page.dart';

class WelComePage extends StatefulWidget {
  const WelComePage({super.key});

  @override
  State<WelComePage> createState() => _WelComePageState();
}

class _WelComePageState extends State<WelComePage> {
  Timer? mTimer;
  var text = "跳过3s";

  void startTimer(int count) {
    mTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
      count--;

      setState(() {
        text = "跳过${count.toString()}s";
      });
      String type = await getDeviceType();
      if (count == 0) {
        mTimer!.cancel();
        if (SharePreUtils.getToken().isNotEmpty) {
          identifyDevice(type);
        } else {
          //进入登录页面
          if (type == "TV") {
            Navigator.of(context).pushAndRemoveUntil(
                MaterialPageRoute(builder: (context) => const LoginTVPage()),
                (route) => false);
          } else {
            Navigator.of(context).pushAndRemoveUntil(
                MaterialPageRoute(
                    builder: (context) => const LoginPhonePadPage()),
                (route) => false);
          }
        }
      }
    });
  }

  @override
  void initState() {
    super.initState();
    startTimer(1); //倒计时3秒
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        height: double.infinity,
        width: double.infinity,
      ),
    );
  }

  Future<String> getDeviceType() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    String? deviceName;

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceName = androidInfo.model;
      bool isTV =
          androidInfo.systemFeatures.contains('android.software.leanback');
      if (isTV == true) {
        return "TV";
      }
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceName = iosInfo.utsname.machine;
    } else if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) {
      // 对于桌面设备，可以通过Platform来判断
      if (Platform.isMacOS) {
        deviceName = 'Apple TV'; // 假定Apple TV是基于macOS的
      } else {
        deviceName = 'Unknown';
      }
    }
    print("当前的设备名称 $deviceName");
    // 基于设备名称判断类型
    if (deviceName!.contains('iPad')) {
      return 'iPad';
    } else if (deviceName.contains('iPhone')) {
      return 'iPhone';
    } else if (deviceName.contains('tv')) {
      return 'TV';
    } else {
      if (Device.get().isTablet) {
        return 'Android Pad';
      } else {
        return 'Android';
      }
    }
  }

  void identifyDevice(String type) {
    //   if (Device.get().isTablet) {
    //     //判断系统
    //     if (Device.get().isIos) {
    //       //iPad
    //     } else {
    //       //平板
    //     }
    //
    //     //直接进入首页  pad版本
    //     Navigator.of(context).pushAndRemoveUntil(
    //         MaterialPageRoute(builder: (context) => const HomeMainPagePad()),
    //         (route) => false);
    //   }
    //   //手机
    //   else if (Device.get().isPhone) {
    //     //直接进入首页  手机版本
    //     Navigator.of(context).pushAndRemoveUntil(
    //         MaterialPageRoute(builder: (context) => const HomeMainPage()),
    //         (route) => false);
    //   } else {
    //     //直接进入TV
    //     Navigator.of(context).pushAndRemoveUntil(
    //         MaterialPageRoute(builder: (context) => const HomeMainPageTV()),
    //         (route) => false);
    //   }
    // }
    print("当前的类型$type");
    if (type == "iPad" || type == "Android Pad") {
      Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(builder: (context) => const HomeMainPagePad()),
          (route) => false);
    } else if (type == "iPhone" || type == "Android") {
      Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(builder: (context) => const HomeMainPhonePage()),
          (route) => false);
    } else if (type == "TV") {
      Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(builder: (context) => const HomeMainPageTV()),
          (route) => false);
    } else {
      Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(builder: (context) => const HomeMainPhonePage()),
          (route) => false);
    }
  }
}
